import { isEmpty } from 'lodash'

/**数据类型转换相关 */
export default () => {
  /**接口数据转换组件数据 */
  const dataToProps = (data, defaultConfig: DiyComponentOptions) => {
    // console.log('🚀 ~ dataToProps ~ defaultConfig:', defaultConfig)
    // console.log('🚀 ~ dataToProps ~ data:', data)
    // TODO
    const config: DiyComponentOptions = {
      content: [],
      style: []
    }
    defaultConfig.content.forEach(item => {
      const _item = {
        ...item,
        defaultValue: data.content[(item as DiyConfigDefaultOption).valueKey],
        dragList: {}
      }
      if (item.type == 'drag-list') {
        ;(item as DiyConfigDragOptions).dragOption!.forEach(element => {
          _item.dragList[(element as DiyConfigDefaultOption).valueKey] = (element as DiyConfigDefaultOption).defaultValue
        })
      }
      config.content.push(_item)
    })
    defaultConfig.style.forEach(item => {
      config.style.push({
        ...item,
        defaultValue: data.style[(item as DiyConfigDefaultOption).valueKey]
      })
    })
    return config
  }
  /**组件数据转换接口数据 */
  const propsToData = componentItem => {
    // console.log('🚀 ~ propsToData ~ componentItem:', componentItem)
    const config: any = {
      content: {},
      style: {},
      name: componentItem.component?.name,
      type: componentItem.component?.type,
      timestamp: componentItem.timestamp,
      label: componentItem.component?.label
    }
    if (!componentItem.component) return config
    componentItem.component.defaultConfig.content?.forEach(item => {
      if (item.valueKey) {
        config.content[item.valueKey] = item.defaultValue
      }
      // 拖拽列表处理
      if (item.type == 'drag-list') {
        config.content.dragList = []

        const _item: any = {}
        item.dragOption!.forEach(element => {
          _item[element.valueKey] = element.defaultValue
        })
        config.content.dragList.push(_item)
      }
    })

    componentItem.component.defaultConfig.style?.forEach(item => {
      config.style[item.valueKey] = item.defaultValue
    })
    return config
  }
  return { dataToProps, propsToData }
}
